import java.util.Arrays;
import java.util.HashMap;

class Solution {
    public int pivotIndex(int[] nums) {

        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (sum * 2 == total - nums[i]) return i;
            sum += nums[i];

        }

        return -1;
    }
}


class Solution1 {

    public static void main(String[] args) {
        int[] arr={1,2,3,4};
        productExceptSelf(arr);
    }


    public static int[] productExceptSelf(int[] nums) {

        int n=nums.length;

        int[] arr=new int[n];

        int[] prev=new int[n];
        int[] tatil=new int[n];

        prev[0]=prev[1]=1;
        tatil[n-1]= tatil[n-2]=1;

        int j=n-2;
        for(int i=1;i<n;i++){
            prev[i]=prev[i-1]*nums[i-1];
            tatil[j]=nums[j+1]*tatil[j+1];
            j--;
        }

        for(int i=0;i<n;i++){
            arr[i]=prev[i]*tatil[i];
        }

        return arr;

    }
}

class Solution2 {
    public int subarraySum(int[] nums, int k) {
        int sum=0,n=nums.length,count=0;
        HashMap<Integer,Integer> hash=new HashMap<>();
        hash.put(0,1);
        for(int i=0;i<n;i++){
            sum+=nums[i];
            int num=sum-k;
            count+=hash.getOrDefault(num,0);
            hash.put(sum,hash.getOrDefault(sum,0)+1);

        }
        return count;

    }
}